package com.dycong.common.leetcode.any;

import com.dycong.common.leetcode.ds.LinkedNode;

import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;

/**
 * 作用描述:
 *
 * @author dycong
 * @date 2020/1/3 9:32
 */
public class LinkedListDeque<T> implements Deque<T> {

    private int size;

    private LinkedNode<T> head;

    private LinkedNode<T> tail;

    public LinkedListDeque() {
        size = 0;
        head = tail = null;
    }

    public static void main(String[] args) {
        LinkedListDeque<Integer> queue = new LinkedListDeque<>();
        for (int i = 0; i < 10; i++) {
            queue.addLast(i);
            System.out.println(queue);
        }
        queue.clear();
        for (int i = 0; i < 10; i++) {
            queue.addFirst(i);
            System.out.println(queue);
        }
    }

    @Override
    public void addFirst(T t) {
        LinkedNode<T> node = new LinkedNode<>(t);
        if (head == null) {
            //队列为空
            head = node;
            tail = node;
        } else {
            node.next = head;
            head = node;
        }
        size++;
    }

    @Override
    public void addLast(T t) {
        LinkedNode<T> node = new LinkedNode<>(t);
        if (tail == null) {
            //队列为空
            tail = node;
            head = node;
        } else {
            tail.next = node;
            tail = node;
        }
        size++;
    }

    @Override
    public boolean offerFirst(T t) {
        return false;
    }

    @Override
    public boolean offerLast(T t) {
        return false;
    }

    @Override
    public T removeFirst() {
        if (isEmpty()) {
            throw new IllegalStateException();
        }
        LinkedNode<T> preHead = head;
        head = head.next;
        if (head == null) {
            tail = null;
        }
        return preHead.val;
    }

    @Override
    public T removeLast() {
        if (isEmpty()) {
            throw new IllegalStateException();
        }
        LinkedNode<T> preTail = tail;
        //todo 双端队列最好还是用双端链表吧

        return null;
    }

    @Override
    public T pollFirst() {
        return null;
    }

    @Override
    public T pollLast() {
        return null;
    }

    @Override
    public T getFirst() {
        return head.val;
    }

    @Override
    public T getLast() {
        return tail.val;
    }

    @Override
    public T peekFirst() {
        return null;
    }

    @Override
    public T peekLast() {
        return null;
    }

    @Override
    public boolean removeFirstOccurrence(Object o) {
        return false;
    }

    @Override
    public boolean removeLastOccurrence(Object o) {
        return false;
    }

    @Override
    public boolean add(T t) {
        return false;
    }

    @Override
    public boolean offer(T t) {
        return false;
    }

    @Override
    public T remove() {
        return null;
    }

    @Override
    public T poll() {
        return null;
    }

    @Override
    public T element() {
        return null;
    }

    @Override
    public T peek() {
        return null;
    }

    @Override
    public void push(T t) {

    }

    @Override
    public T pop() {
        return null;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {
        head = tail = null;
        size = 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public Iterator<T> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T1> T1[] toArray(T1[] a) {
        return null;
    }

    @Override
    public Iterator<T> descendingIterator() {
        return null;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        LinkedNode<T> node = head;
        while (node != null) {
            builder.append(node.val)
                    .append(",");
            node = node.next;
        }
        return builder.toString();
    }
}
